﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.Finance
{
    public partial class ucStatistics : UserControl
    {
        public ucStatistics()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            btnCheck_Click(this.btnCheck, e);
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            float IncomeCash = MoneyClass.Expenses.getCount(Main.CurrentGuid, "0", "0", this.dtpStart.Value.ToString("yyyy-MM-01"), this.dtpEnd.Value.AddMonths(1).ToString("yyyy-MM-01"));
            this.lblIncomeCash.Text = string.Format("{0}元", IncomeCash.ToString("N2"));
            float IncomeCard = MoneyClass.Expenses.getCount(Main.CurrentGuid, "0", "1", this.dtpStart.Value.ToString("yyyy-MM-01"), this.dtpEnd.Value.AddMonths(1).ToString("yyyy-MM-01"));
            this.lblIncomeCard.Text = string.Format("{0}元", IncomeCard.ToString("N2"));
            float IncomeCount = IncomeCash + IncomeCard;
            this.lblIncomeCount.Text = string.Format("{0}元", IncomeCount.ToString("N2"));

            this.zedGraphIncome.GraphPane.CurveList.Clear();
            this.zedGraphIncome.GraphPane.GraphObjList.Clear();
            this.zedGraphIncome.GraphPane.Title.IsVisible = false;
            this.zedGraphIncome.GraphPane.Legend.IsVisible = false;
            ZedGraph.PieItem segment1 = this.zedGraphIncome.GraphPane.AddPieSlice(IncomeCash, Color.Navy, Color.White, 45f, .2, "现金 " + IncomeCash);
            segment1.LabelDetail.FontSpec.Size = 25f;
            ZedGraph.PieItem segment2 = this.zedGraphIncome.GraphPane.AddPieSlice(IncomeCard, Color.Purple, Color.White, 45f, .2, "银行卡 " + IncomeCard);
            segment2.LabelDetail.FontSpec.Size = 25f;
            this.zedGraphIncome.AxisChange();
            this.zedGraphIncome.Refresh();

            float ExpenseCash = MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", "0", this.dtpStart.Value.ToString("yyyy-MM-01"), this.dtpEnd.Value.AddMonths(1).ToString("yyyy-MM-01"));
            this.lblExpenseCash.Text = string.Format("{0}元", ExpenseCash.ToString("N2"));
            float ExpenseCard = MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", "1", this.dtpStart.Value.ToString("yyyy-MM-01"), this.dtpEnd.Value.AddMonths(1).ToString("yyyy-MM-01"));
            this.lblExpenseCard.Text = string.Format("{0}元", ExpenseCard.ToString("N2"));
            float ExpenseCount = ExpenseCash + ExpenseCard;
            this.lblExpenseCount.Text = string.Format("{0}元", ExpenseCount.ToString("N2"));
            float ExpenseOut = MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", "-1", this.dtpStart.Value.ToString("yyyy-MM-01"), this.dtpEnd.Value.AddMonths(1).ToString("yyyy-MM-01"));
            this.lblWithDrawal.Text = string.Format("{0}元", ExpenseOut.ToString("N2"));
            float ExpenseDiff = ExpenseCount - ExpenseOut;
            this.lblExpenseDiff.Text = string.Format("{0}元", ExpenseDiff.ToString("N2"));

            this.zedGraphExpense.GraphPane.CurveList.Clear();
            this.zedGraphExpense.GraphPane.GraphObjList.Clear();
            this.zedGraphExpense.GraphPane.Title.IsVisible = false;
            this.zedGraphExpense.GraphPane.Legend.IsVisible = false;
            ZedGraph.PieItem segment3 = this.zedGraphExpense.GraphPane.AddPieSlice(ExpenseCash, Color.Navy, Color.White, 45f, .2, "现金 " + ExpenseCash);
            segment3.LabelDetail.FontSpec.Size = 25f;
            ZedGraph.PieItem segment4 = this.zedGraphExpense.GraphPane.AddPieSlice(ExpenseCard, Color.Purple, Color.White, 45f, .2, "银行卡 " + ExpenseCard);
            segment4.LabelDetail.FontSpec.Size = 25f;
            this.zedGraphExpense.AxisChange();
            this.zedGraphExpense.Refresh();

            float Diff = IncomeCount - ExpenseCount;
            this.lblDiff.Text = string.Format("{0}元", Diff.ToString("N2"));

            this.zedGraphDiff.GraphPane.CurveList.Clear();
            this.zedGraphDiff.GraphPane.GraphObjList.Clear();
            this.zedGraphDiff.GraphPane.Title.IsVisible = false;
            this.zedGraphDiff.GraphPane.YAxis.Title.IsVisible = false;
            this.zedGraphDiff.GraphPane.XAxis.Title.Text = "金额(元)";
            this.zedGraphDiff.GraphPane.XAxis.Title.FontSpec.Size = 40f;
            this.zedGraphDiff.GraphPane.Legend.IsVisible = false;
            string[] labels = { "收入", "支出" };
            double[] x = { IncomeCount, ExpenseCount };
            ZedGraph.BarItem myCurve = this.zedGraphDiff.GraphPane.AddBar("支出", x, null, Color.Blue);
            myCurve.Bar.Fill = new ZedGraph.Fill(Color.Blue, Color.White, Color.Blue, 90f);
            this.zedGraphDiff.GraphPane.YAxis.Scale.TextLabels = labels;
            this.zedGraphDiff.GraphPane.YAxis.Type = ZedGraph.AxisType.Text;
            this.zedGraphDiff.GraphPane.BarSettings.Base = ZedGraph.BarBase.Y;
            this.zedGraphDiff.GraphPane.YAxis.Scale.FontSpec.Size = 40f;
            this.zedGraphDiff.GraphPane.XAxis.Scale.FontSpec.Size = 40f;
            this.zedGraphDiff.AxisChange();
            this.zedGraphDiff.Refresh();
        }
    }
}
